package com.d3works.apps.supplychain.service.app;

import com.d3works.apps.base.db.AppSupplychain;
import com.d3works.apps.base.db.Company;
import com.d3works.apps.base.db.repo.AppSupplychainRepository;
import com.d3works.apps.base.db.repo.CompanyRepository;
import com.d3works.apps.base.service.app.AppBaseServiceImpl;
import com.d3works.apps.supplychain.db.SupplyChainConfig;
import com.d3works.apps.supplychain.db.repo.SupplyChainConfigRepository;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.inject.persist.Transactional;
import java.util.List;

@Singleton
public class AppSupplychainServiceImpl extends AppBaseServiceImpl implements AppSupplychainService {

  @Inject private AppSupplychainRepository appSupplychainRepo;

  @Inject private CompanyRepository companyRepo;

  @Inject private SupplyChainConfigRepository supplyChainConfigRepo;

  @Override
  public AppSupplychain getAppSupplychain() {
    return appSupplychainRepo.all().fetchOne();
  }

  @Override
  @Transactional
  public void generateSupplychainConfigurations() {

    List<Company> companies = companyRepo.all().filter("self.supplyChainConfig is null").fetch();

    for (Company company : companies) {
      SupplyChainConfig supplyChainConfig = new SupplyChainConfig();
      supplyChainConfig.setCompany(company);
      supplyChainConfigRepo.save(supplyChainConfig);
    }
  }
}
